package com.youease.chris.control
{
	import com.youease.chris.manager.ImageLoader;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.model.proxy.LayerDataProxy;
	import com.youease.chris.model.proxy.MapDataProxy;
	import com.youease.chris.model.proxy.ProjectDataProxy;
	import com.youease.chris.model.vo.BigImageData;
	import com.youease.chris.model.vo.LayerData;
	import com.youease.chris.model.vo.MapData;
	import com.youease.chris.model.vo.ProjectData;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.model.vo.TileData;
	
	import flash.utils.ByteArray;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	public class OpenProjectCommand extends SimpleCommand implements ICommand
	{
		public static const OPEN_PROJECT:String = "open_project";
		public static const OPEN_PROJECT_COMPLETE:String = "open_project_complete";
		private var projectXml:XML;
		private var textureXml:XML;
		
		private var interval:int;
		
		public function OpenProjectCommand()
		{
			super();
		}
		
		override public function execute(notification:INotification):void {
			this.facade.registerCommand(CreateMapCommand.CREATE_MAP,CreateMapCommand);
			this.facade.registerCommand(CreateTextureCommand.CREATE_TEXTURE,CreateTextureCommand);
			
			textureXml = new XML(notification.getBody()[0]);
//			projectXml = new XML(notification.getBody()[1]);
			registerTextures(textureXml);
//			registerDataProxy(projectXml);
			
//			sendNotification(CreateMapCommand.CREATE_MAP,this.facade.retrieveProxy(projectXml.maps[0].map[0].@name));
//			sendNotification(OPEN_PROJECT_COMPLETE);
			interval = setInterval(test,1000,notification.getBody()[1]);
			
		}
		
		private function test(projectTxt:String):void {
			if(ImageLoader.instance.isloading) {
				return;
			}else {
				clearInterval(interval);
			}
			var mapName:String = registerProject(projectTxt);
			//			registerDataProxy(projectXml);
			sendNotification(CreateMapCommand.CREATE_MAP,this.facade.retrieveProxy(mapName));
			sendNotification(OPEN_PROJECT_COMPLETE);
		}
		
		private function registerTextures(xml:XML):void {
			for each(var texture:XML in xml.child("texture")) {
				var textureData:TextureData = new TextureData();
				textureData.type = texture.@type;
				textureData.img = texture.@img;
				textureData.big = texture.@big;
				TexturesManager.loadTexture(textureData);
			}
			
			for each(var bigTexture:XML in xml.child("bigs")[0].children()) {
				var name:String = bigTexture.@img;
				var fileName:String = name.substring(name.indexOf("/")+1,name.length);
				var bigTextureData:BigImageData = new BigImageData(bigTexture.@img,bigTexture.@width,bigTexture.@height);
				TexturesManager.bigTextures[fileName] = bigTextureData;
			}
			
			sendNotification(CreateTextureCommand.CREATE_TEXTURE);
		}
		
		private function registerProject(serializeTxt:String):String {
			var numArr:Array = serializeTxt.split("-");
			var bytes:ByteArray = new ByteArray();
			for(var i:uint = 0;i<numArr.length;i++) {
				bytes[i] = numArr[i];
			}
			bytes.uncompress();
			bytes.position = 0;
			var obj:Object = bytes.readObject();
			
			var projectData:ProjectData = new ProjectData(obj.name);
			var projectDataProxy:ProjectDataProxy = new ProjectDataProxy(projectData);
			for each(var map:Object in obj.maps) {
				var mapData:MapData = new MapData(map.name);
				mapData.crossData = map.crossData;
				mapData.type = map.type;
				mapData.mapRows = map.mapRows;
				mapData.mapCols = map.mapCols;
				mapData.tileWidth = map.tileWidth;
				mapData.tileHeight = map.tileHeight;
				projectDataProxy.addMap(mapData);
				
				var mapDataProxy:MapDataProxy = new MapDataProxy(mapData);
				for each(var layer:Object in map.layers) {
					var layerData:LayerData = new LayerData(layer.name,mapData);
					layerData.depth = layer.depth;
					
					var layerDataProxy:LayerDataProxy = new LayerDataProxy(layerData);
					
					for each(var tile:Object in layer.tiles) {
						var tileData:TileData = new TileData();
						var textTureData:TextureData = new TextureData();
						textTureData.type = tile.textrue.type;
						textTureData.index = tile.textrue.index;
						textTureData.img = tile.textrue.img;
						tileData.textrue = textTureData;
						tileData.gridX = tile.gridX;
						tileData.gridY = tile.gridY;
						
						layerData.tiles[tileData.gridX*mapData.mapCols + tileData.gridY] = tileData;
					}
					
					mapDataProxy.addLayer(layerData);
					
					this.facade.registerProxy(layerDataProxy);
				}
				
//				for each(var crossData:XML in map.child("crossDatas")[0].children()) {
//					mapData.crossData[crossData.@gridX+","+crossData.@gridY] = crossData.@cross == "1"?true:false;
//				}
				
				this.facade.registerProxy(mapDataProxy);
			}
			
			this.facade.registerProxy(projectDataProxy);
			
			return projectData.maps[0].name;
		}
		
		private function registerDataProxy(xml:XML):void {
			
			var projectData:ProjectData = new ProjectData(xml.@name);
			var projectDataProxy:ProjectDataProxy = new ProjectDataProxy(projectData);
			for each(var map:XML in xml.child("maps")[0].children()) {
				var mapData:MapData = new MapData(map.@name);
				mapData.type = map.@type;
				mapData.mapRows = map.@rows;
				mapData.mapCols = map.@cols;
				mapData.tileWidth = map.@tileWidth;
				mapData.tileHeight = map.@tileHeight;
				projectDataProxy.addMap(mapData);
				
				var mapDataProxy:MapDataProxy = new MapDataProxy(mapData);
				for each(var layer:XML in map.child("layers")[0].children()) {
					var layerData:LayerData = new LayerData(layer.@name,mapData);
					layerData.depth = layer.@depth;
					
					var layerDataProxy:LayerDataProxy = new LayerDataProxy(layerData);
					
					for each(var tile:XML in layer.children()) {
						var tileData:TileData = new TileData();
						var textTureData:TextureData = new TextureData();
						textTureData.type = tile.@type;
						textTureData.img = tile.@img;
						tileData.textrue = textTureData;
						tileData.gridX = tile.@gridX;
						tileData.gridY = tile.@gridY;
						
						layerData.tiles[tileData.gridX*mapData.mapCols + tileData.gridY] = tileData;
					}
					
					mapDataProxy.addLayer(layerData);
					
					this.facade.registerProxy(layerDataProxy);
				}
				
				for each(var crossData:XML in map.child("crossDatas")[0].children()) {
					mapData.crossData[crossData.@gridX+","+crossData.@gridY] = crossData.@cross == "1"?true:false;
				}
				
				this.facade.registerProxy(mapDataProxy);
			}
			
			this.facade.registerProxy(projectDataProxy);
		}
	}
}